package com.polaris.live.dialog

import android.content.Context
import android.view.View
import com.blankj.utilcode.util.ScreenUtils
import com.bumptech.glide.Glide
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.databinding.DialogPartyTaskBinding
import com.polaris.live.im.sfs.bean.pusher.NoticeTaskCompleteMessage
import com.polaris.live.manager.LiveStateManager
import com.polaris.live.ui.game.GameUtils
import com.polaris.live.ui.party.PartyUserStateUtil
import com.polaris.live.ui.party.activity.PartyAnchorActivity
import com.polaris.live.ui.party.activity.game.PartyGameAnchorActivity
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.route.RouterUtils

/**
 * PartyTaskDialog
 *
 * @author Created by 半仙 on 2024/3/1/001 17:42
 */
class PartyTaskDialog(
    context: Context,
    private val mNoticeTask: NoticeTaskCompleteMessage,
) : CenterPopupView(context) {
    override fun getImplLayoutId() = R.layout.dialog_party_task

    private lateinit var mBinding: DialogPartyTaskBinding
    override fun onCreate() {
        super.onCreate()
        mBinding = DialogPartyTaskBinding.bind(popupImplView)

        mBinding.apply {
            tvTitleDetail.text = mNoticeTask.title
            Glide.with(context).load(ImageUtils.imgPath(mNoticeTask.rewardPhotoUrl))
                .into(ivTitleImg)
            tvTitleCount.text = mNoticeTask.rewardContent
            taskDetail.text = mNoticeTask.rewardExplain
            goTo.text = mNoticeTask.confirm.title

            if (mNoticeTask.cancel == null) {
                tvCancel.visibility = View.GONE
            } else {
                tvCancel.text = mNoticeTask.cancel.title
            }

            tvCancel.setOnSingleClickListener {
                dismiss()
            }

            goTo.setOnSingleClickListener {
                val aliveTopActivity = ViewUtils.getAliveTopActivity() ?: dismiss()
                if (LiveStateManager.isCalling()
                    || LiveStateManager.isLiving()
                    || aliveTopActivity is PartyGameAnchorActivity
                    || aliveTopActivity is PartyAnchorActivity
                    || GameUtils.gameRun
                    || PartyUserStateUtil.inTheGame || PartyUserStateUtil.inTheGameSeat || PartyUserStateUtil.inTheMaiSeat
                ) {
                    dismiss()
                    return@setOnSingleClickListener
                }
                if (mNoticeTask.confirm.router?.isNotEmpty() == true) {
                    RouterUtils.routerGoto(mNoticeTask.confirm.router)
                }

                dismiss()
            }
        }
    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }
}